package com.zhuss.${moudle}.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.${moudle}.${tablePkg}.Search${poEntity}Bo;
import com.zhuss.bean.vo.${moudle}.${tablePkg}.List${poEntity}Vo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.${moudle}.db.dao.ext.${poEntity}Dao;
import com.zhuss.${moudle}.db.entity.${poEntity};
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;


/**
 * @description: ${tableComment}读
 * @author: zhushanshuo
 * @create: ${creatTime}
 **/
@Component("${poEntityLower}Read")
public class ${poEntity}Read {
	
	@Autowired
	private ${poEntity}Dao ${poEntityLower}Dao;
	
	
	public Map<String, ${poEntity}> listMap() {
		List<${poEntity}> list = ${poEntityLower}Dao.listAll();
		Map<String, ${poEntity}> map = new HashMap<>();
		for (${poEntity} c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}
	
	/**
	 * ${tableComment}
	 * @return
	 */
	public Object list${poEntity}() {
		List<${poEntity}> ${poEntityLower}List = ${poEntityLower}Dao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(${poEntityLower}List, result);
		return result;
	}
	
	
	/**
	 * ${tableComment}
	 * @return
	 */
	public Object list${poEntity}(List<Long> ${poEntityLower}IdList) {
		List<${poEntity}> result = ${poEntityLower}Dao.listById(${poEntityLower}IdList);
		return result;
	}
	
	
	/**
	 * 根据名字查询${tableComment}
	 * @param 
	 * @return
	 */
	public List<${poEntity}> list(String name) {
		List<${poEntity}> result = ${poEntityLower}Dao.listByName(name);
		return result;
	}
	
	public List<${poEntity}> listNotId(String name, Long ${poEntityLower}Id) {
		List<${poEntity}> result = new ArrayList<${poEntity}>();
		List<${poEntity}> list = list(name);
		for (${poEntity} c : list) {
			if (!c.get${poEntity}Id().equals(${poEntityLower}Id)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * ${tableComment} 
	 * @return
	 */
	public Object list${poEntity}Id() {
		List<${poEntity}> ${poEntityLower}List = ${poEntityLower}Dao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(${poEntityLower}List, result);
		return result;
	}
	
	private void transferListId(List<${poEntity}> ${poEntityLower}List, List<Long> result) {
		for (${poEntity} item : ${poEntityLower}List) {
			result.add(item.get${poEntity}Id());
		}
	}

	private void transferList(List<${poEntity}> ${poEntityLower}List, List<ListVo> result) {
		for (${poEntity} item : ${poEntityLower}List) {
			ListVo vo = new ListVo();
			vo.setKey(item.get${poEntity}Id());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * ${tableComment}列表
	 * @param bo
	 * @return
	 */
	public Object list(Search${poEntity}Bo bo) {
		PageVo<List${poEntity}Vo> result = new PageVo<List${poEntity}Vo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(${poEntityLower}Dao.list(bo));
        result.setTotal(${poEntityLower}Dao.countList(bo));
		return result;
	}
	
	/**
	 * ${tableComment}详情
	 * @param ${poEntityLower}Id
	 * @return
	 * @throws AabException
	 */
	public Object detail(Long ${poEntityLower}Id) throws AabException {
		Object result = ${poEntityLower}Dao.detail(${poEntityLower}Id);
		if (result == null) {
			throw new AabException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
